﻿using EmployeeLeaveSystem.Shared.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.Contracts;

namespace EmployeeLeaveSystem.Server.Controllers.OvertimeController {
    [Route("api/OverTime/[action]")]
    [ApiController]
    public class OverTimeController : ControllerBase {
        readonly EpMainContext _context;
        public OverTimeController(EpMainContext context) {
            _context = context;
        }
        // 新增一条加班申请记录
        [HttpPost]
        public ActionResult AddOverTime(AddWOM addWOM) {
            addWOM.Created = DateTime.Now; addWOM.AcceptTag = AddWOMAcceptTag.审核中;
            _context.AddWOMs.Add(addWOM);
            _context.SaveChanges();
            return Ok();
        }
        [HttpGet]
        public List<AddWOM> GetOverTime(int usrId) {
            return _context.AddWOMs.Include(x => x.AcceptStaff).Where(x => x.StaffId.Equals(usrId)).ToList();
        }
        // 查询特定的相关记录
        [HttpGet]
        public AddWOM? GetOverTimeById(int id) {
            return _context.AddWOMs.Include(x => x.Staff).Include(x => x.AcceptStaff).Where(x => x.Id.Equals(id)).FirstOrDefault();
        }
        // 查询特定的相关记录2
        [HttpGet]
        public AddWOM? GetOverTimeById2(int id) {
            return _context.AddWOMs.Where(x => x.Id.Equals(id)).Include(x => x.Staff).Include(x => x.AcceptStaff).FirstOrDefault();
        }
        // 查询需要我审核的记录
        [HttpGet]
        public List<AddWOM> GetWOMsByManagerUsrId(int id) {
            return _context.AddWOMs.Where(x => x.AcceptStaffId == id).Include(x => x.Staff).ToList();
        }
        // 需要更新的记录
        public ActionResult Update(AddWOMAcceptTag acceptFlag, int id) {
            var enty = _context.AddWOMs.Where(x => x.Id.Equals(id)).FirstOrDefault();
            enty.AcceptTag = acceptFlag;
            _context.AddWOMs.Update(enty);
            _context.SaveChanges();
            return Ok();
        }
    }
}
